<?php
/**
 * 人脸更新记录表
 */
namespace App\Model\Face;

use App\Model\BaseModel;
use App\Model\Device\Group as GroupModel;
use App\Model\Device\DeviceModel;
use App\Model\Device\DeviceInfoModel;

class UpdateFace extends BaseModel
{

	public $tableName = "sc_update_face";

	/**
	 * 通过条件获取数据库里面的设备
	 * @auth   xiaozhi
	 * @param  array   $condition [description]
	 * @return [type]             [description]
	 */
	public function getUpdateFaceData($field = '*', $where = [],  $page = 1, $pageSize =10, $order = ['A.id', 'desc'])
    {


		$field = 'A.id,A.imgpath,A.name,A.numbers,A.deviceremark,A.mode,A.operatingtime,A.createtime,A.operating,A.operatingcode,A.operatingstatus,A.operatinginfo,B.name as devicename,C.name as groupname';


		$this->withTotalCount()
		    ->field($field)
		    ->order($order[0], $order[1])
		    ->alias('A')
			->join('mymk_device as B','A.deviceremark=B.deviceremark','LEFT')
			->join('mymk_device_group as C','B.groupid=C.id','LEFT')
			->where($where)
            ->page($page, $pageSize)
            ->all();
		

		$count = $this->lastQueryResult()->getTotalCount();
        $lists = $this->lastQueryResult()->getResult();

        // $list = $lastResult->getResult();
        return ['count' => $count, 'lists' => $lists];

	}

	/**
	 * 根据条件获取人脸更新列表数据
	 * @param  [type] $columns [description]
	 * @param  [type] $where   [description]
	 * @param  [type] $limit   [description]
	 * @return [type]          [description]
	 */
	public function getScheduleData($field, $where, $pageSize=10){


		$faceObj = new FaceModel();
		$faceInfoObj = new FaceInfoModel();
		$deviceObj = new DeviceModel();
		$deviceInfoObj = new DeviceInfoModel();

		$this->withTotalCount()
		    ->field($field)
		    ->alias('A')
			->join($faceObj->tableName.' B', 'A.faceid=B.id', 'left')
			->join($deviceObj->tableName.' C', 'A.devid=C.id', 'left')
			->join($faceInfoObj->tableName.' D', 'A.faceid=D.faceid', 'left')
			->join($deviceInfoObj->tableName.' E', 'A.devid=E.devid', 'left')
			->where($where)
            ->page(1, $pageSize)
            ->all();

        $lists = $this->lastQueryResult()->getResult();


        return $lists;
		if(is_array($where)){
            foreach ($where as $value) {
                $this->db->where($value[0],$value[1],$value[2]);
            }
        }

		$this->db->join($faceObj->tableName.' B','A.faceid=B.id','left');
		$this->db->join($deviceObj->tableName.' C','A.devid=C.id','left');
		$this->db->join($faceInfoObj->tableName.' D','A.faceid=D.faceid','left');
		$this->db->join($deviceInfoObj->tableName.' E','A.devid=E.devid','left');
		$this->db->orderBy("C.id", "desc");

		$res = $this->db->get($this->tableName.' A', $limit, $columns);
		return $res;
	}

    /**
     * 根据条件获取人脸更新列表数据
     * @param  [type] $columns [description]
     * @param  [type] $where   [description]
     * @param  [type] $limit   [description]
     * @return [type]          [description]
     */
    public function getScheduleOne($field, $where){
        $faceObj = new FaceModel();
        $faceInfoObj = new FaceInfoModel();
        $deviceObj = new DeviceModel();
        $deviceInfoObj = new DeviceInfoModel();

        $this->withTotalCount()
            ->field($field)
            ->alias('A')
            ->join($faceObj->tableName.' B', 'A.faceid=B.id', 'left')
            ->join($deviceObj->tableName.' C', 'A.devid=C.id', 'left')
            ->join($faceInfoObj->tableName.' D', 'A.faceid=D.faceid', 'left')
            ->join($deviceInfoObj->tableName.' E', 'A.devid=E.devid', 'left')
            ->where($where)
            ->get();

        $result = $this->lastQueryResult()->getResult();
        if($result){
            return $result[0];
        }else{
            return  false;
        }
    }
}